package com.lframework.xingyun.basedata.controller;

import com.lframework.starter.web.core.annotations.security.HasPermission;
import com.lframework.starter.web.core.controller.DefaultBaseController;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.components.resp.InvokeResult;
import javax.validation.constraints.NotBlank;

import com.lframework.xingyun.basedata.bo.shop.GetShopProductBo;
import com.lframework.xingyun.basedata.bo.shop.QueryShopProductBo;
import com.lframework.xingyun.basedata.entity.ShopProduct;
import com.lframework.xingyun.basedata.service.shop.ShopProductService;
import com.lframework.xingyun.basedata.vo.shop.QueryShopProductVo;
import com.lframework.xingyun.basedata.vo.shop.SaveShopProductVo;
import io.swagger.annotations.ApiImplicitParam;
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import io.swagger.annotations.ApiOperation;
import com.lframework.starter.common.utils.CollectionUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 门店商品关系表 Controller
 *
 */
@Api(tags = "门店商品关系表")
@Validated
@RestController
@RequestMapping("/basedata/shop/product")
public class ShopProductController extends DefaultBaseController {

    @Autowired
    private ShopProductService shopProductService;

    /**
     * 查询列表
     */
    @ApiOperation("查询列表")
    @HasPermission({"base-data:shop-product:query"})
    @GetMapping("/query")
    public InvokeResult<PageResult<QueryShopProductBo>> query(@Valid QueryShopProductVo vo) {

        PageResult<ShopProduct> pageResult = shopProductService.queryPage(getPageIndex(vo), getPageSize(vo), vo);

        List<ShopProduct> datas = pageResult.getDatas();
        List<QueryShopProductBo> results = null;

        if (!CollectionUtil.isEmpty(datas)) {
            results = datas.stream().map(QueryShopProductBo::new).collect(Collectors.toList());
        }

        return InvokeResultBuilder.success(PageResultUtil.rebuild(pageResult, results));
    }

    /**
     * 根据ID查询
     */
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
    @HasPermission({"base-data:shop-product:query"})
    @GetMapping
    public InvokeResult<GetShopProductBo> get(@NotBlank(message = "id不能为空！") String id) {

        ShopProduct data = shopProductService.findById(id);
        if (data == null) {
            throw new DefaultClientException("门店商品关系表不存在！");
        }

        GetShopProductBo result = new GetShopProductBo(data);

        return InvokeResultBuilder.success(result);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @HasPermission({"base-data:shop-product:save"})
    @PostMapping
    public InvokeResult<Void> save(@Valid @RequestBody SaveShopProductVo vo) {

        shopProductService.save(vo);

        return InvokeResultBuilder.success();
    }
}

